import Taro from '@tarojs/taro'
import { baseUrl } from '@/utils/config'
import * as publicRequest from './public'
import * as userRequest from './user'
import * as groupRequest from './group'
import * as attrRequest from './attr'

export const request = (url: string, data: Record<string, any>, method: keyof Taro.request.Method) => {
    return new Promise<Taro.request.SuccessCallbackResult<any>>((resolve, reject) => {
        Taro.request({
            url: baseUrl + url,
            method,
            data: {
                ...data,
                env: Taro.getStorageSync('env')
            },
            header: {
                'content-type': 'application/json',
                'Authorization': 'Bearer ' + Taro.getStorageSync('token')
            },
            success: (r) => {
                if (r.data.code === 200) resolve(r)
                else reject(r)
            },
            fail(r) {
                reject(r)
            }
        })
    })
}

export const $http = {
    ...publicRequest,
    ...userRequest,
    ...groupRequest,
    ...attrRequest
}
